<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>考题管理</title>
	<meta name="decorator" content="default"/>
	<script src="${path}/js/common/jquery.form.js"></script>

	<script type="text/javascript">
		$(document).ready(function() {
			//$("#name").focus();

			if ($("#questionTypeTwo").val() == "1") {
//				alert(1)
				$("#answer_add_del").show();
			} else {
				$("#answer_add_del").hide();
			}


			var type="${resourceQuestion.questionType}";

			if(type=='3') {
				$("#optionABCD").hide();
			}
			if(type=='2')
				$("#optionCD").hide();


			$("#inputForm").validate({
				submitHandler: function(form){
					loading('正在提交，请稍等...');
					form.submit();
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});
		});
		$(function () {
			$("#fileUpload").click(function () {
//				alertx("点击");

				$.jBox($("#importBox").html(), {
					title: "上传附件", buttons: {"关闭": true},
					bottomText: "仅允许上传“doc、docx、pdf”格式文件！",
					loaded: function (e) {
						e.find(".btnImportSubmit").click(function () {
							if (e.find("#uploadFile").val() == "") {
								alertx("请选择附件上传");
								return false;
							}
							var s = window.parent.document.getElementById("attachmentMaskDIV");
							s.style.display = "block";
							e.find("#importForm").ajaxSubmit({
								type: "POST",
								dataType: "json",
								url: "${ctx}/resource/resourceQuestion/fileUpload",
								success: function (json) {
									if (json.success == true) {
										var fileList = json.dataObj;
										$("#attachment").val(fileList[0]);
										$("#attachmentUrl").val(fileList[1]);
										alertx("上传成功");
										var s = window.parent.document.getElementById("attachmentMaskDIV");
										s.style.display = "none";
										e.parent().next().children("button").click();
									}
									else {
										var s = window.parent.document.getElementById("attachmentMaskDIV");
										s.style.display = "none";
										window.location.href = "${ctx}/resource/resourceCourseware/form";
									}
								},
								error: function (XMLHttpRequest, textStatus, errorThrown) {
									var s = window.parent.document.getElementById("attachmentMaskDIV");
									s.style.display = "none";
									alertx(errorThrown);
								}
							});

						})
					}

				});
			});
		});


		function addFile(files) {
			var questionTypeObj=document.getElementById("questionType");
			var questionTypeValue=questionTypeObj.options[questionTypeObj.selectedIndex].value;
			if(questionTypeValue!='0')
			{
				var num=0;


				$("#answer_add input[type='text']").each(function(){
					num++;
				});


				if(num<3)
				{
					var content = "<div class='controls' style='margin-top:8px'>";
					content += "<input type='text' htmlEscape='false'  maxlength='1' onkeyup=\"this.value=this.value.replace(/[^a-d]/g,'')\" class='input-xlarge'/>";
//			content += "<input name='"+name+"'type='file' accept='image/gif,image/png,image/jpg,image/jpeg' onchange='picCheck(this)' required/>";
					content += "<button class='' style='margin-left: 5px' type='button'onclick='removeFile(this)'>-</button>";
					content += "</div>"
					$(files).append(content);
				}
			}




		}

		//删除当前行
		function removeFile(obj) {
			$(obj).parent().remove();
		}
		function showAnswer() {
		var type = document.getElementById("questionType").value;



			var str = new Array();
//			var empty=false;
//			if(document.getElementById("questionAnswer").value!=''){
//				str = document.getElementById("questionAnswer").value.split(",");
//			}
			document.getElementById("questionAnswer").value='';    //把获取到的文件ID传值给html
			$("#answer_div input[type='text']").each(function(){
				if($(this).val()!='')
					str.push($(this).val());
//				else
//				{
//					empty=true;
//				}

			});

			if(str=='')
			{
				alertx("答案不能为空")
				return false;
			}
			if(type=='1'){
			if(str.length==1)
			{
				alertx("多选题不能只填一个答案")
				return false;

			}
			}
			document.getElementById("questionAnswer").value=str;    //把获取到的文件ID传值给html

		}
		function questionTypeSelect(type)
		{
			//清空答案选项和div内容
//			var answer=document.getElementById("answer").onkeyup=function() {};
			document.getElementById("answer").value="";
			if(type.options[type.selectedIndex].value==3)
			{
				document.getElementById("answer").value="";
				document.getElementById("answer1").value="";
				document.getElementById("tishi").style.display="none";
				document.getElementById("answer").style.display="none";
				document.getElementById("answer1").style.display="";



			}
			else{
				document.getElementById("answer1").value="";
				document.getElementById("answer").value="";

				document.getElementById("tishi").style.display="";
				document.getElementById("answer1").style.display="none";
				document.getElementById("answer").style.display="";


			}
			document.getElementById("questionAnswer").value='';

			$("#answer_add").empty();
			if (type.options[type.selectedIndex].value == 1) {
				$("#answer_add_del").show();
			} else {
				$("#answer_add_del").hide();
			}




			if(type.options[type.selectedIndex].value==2)
			{
				$("#optionABCD").show();
				$("#optionCD").hide();
			}else if(type.options[type.selectedIndex].value==3)
			{
				$("#optionABCD").hide();
			}else{
				$("#optionABCD").show();

				$("#optionAB").show();
				$("#optionCD").show();
			}




		}
		function picCheck(selector) {

			var fileName = $(selector).val();

			if (fileName != null && fileName != "") {
				var fileExt = fileName.substr(fileName.lastIndexOf(".")).toLowerCase();
				var fileType1 = new Array(".pdf");
				var fileType2 = new Array(".doc");
				var fileType3 = new Array(".docx");
				//lastIndexOf如果没有搜索到则返回为-1
				if ($.inArray(fileExt, fileType1) == -1 && $.inArray(fileExt, fileType2) == -1 &&$.inArray(fileExt, fileType3) == -1) {
					var file = $(selector);
					file.after(file.clone().val(""));
					file.remove();
					alertx("附件格式不正确，只能是pdf、doc、docx格式");
				}

			}
		}

	</script>
</head>
<body>

<%--判断修改进来时是否多选类型--%>
<input type="hidden" value="${questionTypeTwo}" id="questionTypeTwo">


<div id="importBox" class="hide">
	<form id="importForm" name="importForm" enctype="multipart/form-data"
		  class="form-search" style="padding-left:20px;text-align:center;"><br/>
		<input id="uploadFile" name="uploadFile" type="file" style="width:330px" onchange="picCheck(this)"/><br/><br/>　　
		<button type="button" class="btn btn-primary btnImportSubmit"> 上 传</button>
	</form>
</div>
<ul class="nav nav-tabs">
	<li><a href="${ctx}/resource/resourceQuestion/">考题列表</a></li>
	<li class="active"><a href="${ctx}/resource/resourceQuestion/form?id=${resourceQuestion.id}">考题<shiro:hasPermission name="resource:resourceQuestion:edit">${not empty resourceQuestion.id?'修改':'添加'}</shiro:hasPermission><shiro:lacksPermission name="resource:resourceQuestion:edit">查看</shiro:lacksPermission></a></li>
</ul><br/>
<form:form id="inputForm" modelAttribute="resourceQuestion" action="${ctx}/resource/resourceQuestion/save" method="post" class="form-horizontal">
	<form:hidden path="id"/>
	<sys:message content="${message}"/>
	<div class="control-group">
		<label class="control-label">考题名称：</label>
		<div class="controls">
			<form:input path="questionName" htmlEscape="false" maxlength="64" class="input-xlarge required"/>
			<span class="help-inline"><font color="red">*</font> </span>
		</div>
	</div>
	<div class="control-group">
		<label class="control-label">考题属性：</label>
		<div class="controls">
			<%--<select id="questionAttribute" name="questionAttribute" class="input-xlarge required" <c:if test="${isSuperAdmin=='false'}">disabled</c:if>>--%>
				<%--<c:forEach var="attribute" items="${fns:getDictList('resource_question_attribute')}">--%>
					<%--<option value="${fns:getDictValue(attribute,'resource_question_attribute',"")}" <c:if test="${fns:getDictValue(attribute,'resource_question_attribute','')==resourceQuestion.questionAttribute}">selected</c:if>>${attribute}</option>--%>
				<%--</c:forEach>--%>
			<%--</select>--%>
				<select id="questionAttribute" name="questionAttribute" class="input-xlarge required">
					<c:choose>
						<c:when test="${isSuperAdmin=='false'}">
							<option value="0" selected>${fns:getDictLabel("0",'resource_question_attribute',"无")}</option>
						</c:when>
						<c:otherwise>
							<c:forEach var="attribute" items="${fns:getDictList('resource_question_attribute')}">
								<option value="${fns:getDictValue(attribute,'resource_question_attribute',"")}" <c:if test="${fns:getDictValue(attribute,'resource_question_attribute','')==resourceQuestion.questionAttribute}">selected</c:if>>${attribute}</option>
							</c:forEach>
						</c:otherwise>
					</c:choose>
				</select>



		<%--<form:select path="questionAttribute" class="input-xlarge required">--%>
				<%--<form:option value="" label=""/>--%>
				<%--<form:options items="${fns:getDictList('resource_question_attribute')}" itemLabel="label" itemValue="value" htmlEscape="false"/>--%>
			<%--</form:select>--%>
			<span class="help-inline"><font color="red">*</font> </span>
		</div>
	</div>
	<div class="control-group">
		<label class="control-label">标签：</label>
		<div class="controls">
			<form:input path="label" htmlEscape="false" maxlength="64" class="input-xlarge "/>
		</div>
	</div>
	<div class="control-group">
		<label class="control-label">题目类型：</label>
		<div class="controls">

			<select id="questionType" name="questionType" class="input-xlarge required" onchange="questionTypeSelect(this)">
				<c:forEach var="attribute" items="${fns:getDictList('resource_question_type')}">
					<option value="${fns:getDictValue(attribute,'resource_question_type',"")}" <c:if test="${fns:getDictValue(attribute,'resource_question_type','')==resourceQuestion.questionType}">selected</c:if>>${attribute}</option>
				</c:forEach>
			</select>

			<%--<form:select path="questionType" class="input-xlarge required" onChange="questionTypeSelect(this)">--%>
				<%--<form:option value="" label=""/>--%>
				<%--<form:options items="${fns:getDictList('resource_question_type')}" itemLabel="label" itemValue="value" htmlEscape="false"/>--%>
			<%--</form:select>--%>
			<span class="help-inline"><font color="red">*</font> </span>
		</div>
	</div>
	<div class="control-group">
		<label class="control-label">题目难度：</label>
		<div class="controls">
			<form:select path="questionDifficulty" class="input-xlarge required">
				<form:option value="" label=""/>
				<form:options items="${fns:getDictList('resource_question_difficulty')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
			</form:select>
			<span class="help-inline"><font color="red">*</font> </span>
		</div>
	</div>
	<div class="control-group">
		<label class="control-label">题干：</label>
		<div class="controls">
			<form:textarea path="stem" htmlEscape="false" rows="4" maxlength="255" class="input-xxlarge required" />
			<span class="help-inline"><font color="red">*</font> </span>

		</div>
	</div>
	<div class="control-group" id="optionABCD">
		<label class="control-label">选项：</label>
		<div class="controls">
			<span id="optionAB">
			<label>a:</label>
			<input type="text" name="optionA" id="optionA" htmlEscape="false"  class="input-xlarge " value="${option.optionA}" required/>
			<label>b:</label>
			<input type="text" name="optionB" id="optionB" htmlEscape="false" maxlength="256" class="input-xlarge " value="${option.optionB}" required/>
			</span>
			<span id="optionCD">
			<label>c:</label>
			<input type="text" name="optionC" id="optionC" htmlEscape="false" maxlength="256" class="input-xlarge " value="${option.optionC}" required/>
			<label>d:</label>
			<input type="text" name="optionD" id="optionD" htmlEscape="false" maxlength="256" class="input-xlarge " value="${option.optionD}" required/>
			</span>

		</div>
	</div>
	<input type="hidden" name="questionAnswer" id="questionAnswer">
	<div class="control-group" id="answer_div">
		<label class="control-label">答案：</label>
		<div class="controls">
			<c:choose>
				<c:when test="${resourceQuestion.questionType=='3'}">

				</c:when>
				<c:otherwise>

				</c:otherwise>
			</c:choose>

			<c:if test="${questionTypeTwo == null || questionTypeTwo == ''}">
			<input type="text" id="answer" name="answer" htmlEscape="false" maxlength="1" onkeyup="this.value=this.value.replace(/[^a-d]/g,'')" class="input-xlarge" <c:if test="${resourceQuestion.questionType!='3'}">value="${answer}"</c:if> <c:if test="${resourceQuestion.questionType=='3'}">style="display:none"</c:if>/>
				<input type="text" id="answer1" name="answer1" htmlEscape="false" maxlength="255" class="input-xlarge" <c:if test="${resourceQuestion.questionType=='3'}">value="${answer}"</c:if> <c:if test="${resourceQuestion.questionType!='3'}">style="display:none"</c:if>/>
			</c:if>

				<%--修改  多个答案 的第一个--%>
			<c:if test="${questionTypeTwo != null && questionTypeTwo != ''}">
				<c:forEach items="${questionAnswerList}" var="questionAnswer" varStatus="questionAnswera">
					<c:if test="${questionAnswera.index == 0}">
						<input type="text" id="answer" name="answer" htmlEscape="false" maxlength="1"
							   onkeyup="this.value=this.value.replace(/[^a-d]/g,'')" class="input-xlarge"
							   value="${questionAnswer.answer}"/>
					</c:if>
				</c:forEach>
			</c:if>

			<span id="answer_add_del" hidden>
				<button class="" type="button"  onclick="addFile(answer_add)">+</button>
		    	</span>
			<label <c:if test="${resourceQuestion.questionType=='3'}">style="display:none"</c:if> style="color:red;" name="tishi" id="tishi">请输入小写英文字母</label>

		</div>

			<%--修改  多个答案--%>
		<div class="control-group" id="answer_add">
			<c:if test="${questionTypeTwo != null && questionTypeTwo != ''}">
				<c:forEach items="${questionAnswerList}" var="questionAnswer" varStatus="questionAnswera">
					<c:if test="${questionAnswera.index != 0}">
						<div class='controls' style='margin-top:8px'>
							<input type='text' value="${questionAnswer.answer}" htmlEscape='false' maxlength='1'
								   onkeyup="this.value=this.value.replace(/[^a-d]/g,'')" class='input-xlarge'/>
							<button class='' style='margin-left: 5px' type='button' onclick='removeFile(this)'>-
							</button>
						</div>
					</c:if>
				</c:forEach>
			</c:if>
		</div>
	</div>

	<div class="control-group">
		<label class="control-label">关联教学实验环境：</label>
		<div class="controls">
			<form:select path="environmentId" class="input-xlarge ">
				<option value="" selected>====请选择====</option>
				<c:forEach items="${sceneEnvironmentList}" var="sceneEnvironment">
					<option value="${sceneEnvironment.id}" <c:if test="${sceneEnvironment.id==resourceQuestion.environmentId}"> selected</c:if>>${sceneEnvironment.environmentName}</option>
				</c:forEach>
			</form:select>
		</div>
	</div>
	<div class="control-group">
		<label class="control-label">实验文档：</label>
		<div class="controls">
			<form:textarea htmlEscape="false" path="document" rows="4" id="content" maxlength="2000" class="input-xxlarge"/>
			<sys:ckeditor replace="document" uploadPath="/itemDescribe" />
		</div>
	</div>
	<div class="control-group">
		<label class="control-label">上传附件：</label>
		<div class="controls">
			<input type="hidden" name="attachment" id="attachment" htmlEscape="false" maxlength="256" class="input-xlarge" value="" readonly="true"/>
			<input type="text" name="attachmentUrl" id="attachmentUrl" htmlEscape="false" maxlength="256" class="input-xlarge" value="${attachmentUrl}" readonly="true"/>
			<input id="fileUpload" class="btn btn-primary" type="button" value="上传"/>
		</div>
	</div>
	<div class="form-actions">
		<shiro:hasPermission name="resource:resourceQuestion:edit"><input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存" onclick="return showAnswer()"/>&nbsp;</shiro:hasPermission>
		<input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
	</div>
</form:form>
</body>
</html>